<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            托码与箱码绑定
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
          <li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">发货单</div>
                  <input type="text" class="aui-input" placeholder="请扫描发货单" style="width: 80%;" id="fhd" onclick="getIdSelector();">
                  <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="toScanner(3);"></i>
              </div>
          </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">托码</div>
                    <input type="text" class="aui-input" placeholder="请扫描托码" style="width: 80%;" id="trayyinfoid" onclick="getIdSelector();">
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="toScanner(1);"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱码</div>
                    <input type="text" class="aui-input" placeholder="请扫描箱码" style="width: 80%;" id="tm" onclick="getIdSelector();">
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="toScanner(2);"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">供应商编码</div>
                    <input type="text" class="aui-input" disabled="true" id="supplierCode">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <input type="text" class="aui-input" disabled="true" id="mrlCode">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <input type="text" class="aui-input" disabled="true" id="lotCode">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量</div>
                    <input type="text" class="aui-input" disabled="true" id="qty">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <input type="text" class="aui-input" disabled="true" id="boxnum">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">操作 </div>
                  <div class="aui-list-item-input">
                      <label><input class="aui-radio" type="radio" value="绑定" name="type" checked>绑定</label>
                      <label><input class="aui-radio" type="radio" value="解绑" name="type">解绑</label>
                  </div>
                </div>
            </li>
        </ul>

        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">提交</div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    var scanRule = ""; //条码解析规则

    //初始化查询仓库下拉框
    apiready = function() {

        $("#trayyinfoid").focus();

        //查询条码解析规则
        initScanRule();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //箱码绑定
    function save() {
        var fhd = $("#fhd").val();
        var trayyinfoid = $("#trayyinfoid").val();
        var tm = $("#tm").val();
        var supplierCode = $("#supplierCode").val();
        var mrlCode = $("#mrlCode").val();
        var lotCode = $("#lotCode").val();
        var qty = $("#qty").val();
        var boxnum = $("#boxnum").val();
        var type = $("input[name='type']:checked").val();

        //发货单
        if (fhd == "" || fhd == null || fhd == undefined) {
            api.toast({
                msg: '请扫描发货单！',
                duration: 3000
            });
            return;
        }

        //托码
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            api.toast({
                msg: '请扫描托码！',
                duration: 3000
            });
            return;
        }

        //条码
        if (tm == "" || tm == null || tm == undefined) {
            api.toast({
                msg: '请扫描条码！',
                duration: 3000
            });
            return;
        }
        //物料号非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            api.toast({
                msg: '物料号不能为空！',
                duration: 3000
            });
            return;
        }
        //批次号非空校验
        if (lotCode == "" || lotCode == null || lotCode == undefined) {
            api.toast({
                msg: '批次号不能为空！',
                duration: 3000
            });
            return;
        }
        //批次号非空校验
        if (qty == "" || qty == null || qty == undefined || parseFloat(qty) <= 0) {
            api.toast({
                msg: '数量必须大于0！',
                duration: 3000
            });
            return;
        }

        //箱号非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '箱号不能为空！',
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmTrayinfoFhdBind";
        var data = {
            values: {
              fhd: fhd,//发货单号
                trayyinfoid: trayyinfoid, //托码
                tm: tm, //条码
                mrlCode: mrlCode, //物料号
                lotCode: lotCode, //批次号
                qty: qty, //数量
                boxnum: boxnum, //箱号
                type: type,//操作
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {

                    api.toast({
                        msg: "操作成功",
                        duration: 3000
                    });

                    //清空表单数据
                    $("#tm").val("");
                    $("#supplierCode").val("");
                    $("#mrlCode").val("");
                    $("#lotCode").val("");
                    $("#qty").val("");
                    $("#boxnum").val("");
                    //光标回到条码
                    $("#tm").focus();

                } else {

                    $("#tm").val("");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                $("#tm").val("");
                alert(JSON.stringify(err));
            }
        });
    }

    //自动保存
    function saveAuto() {
        var fhd = $("#fhd").val();
        var trayyinfoid = $("#trayyinfoid").val();
        var tm = $("#tm").val();
        var supplierCode = $("#supplierCode").val();
        var mrlCode = $("#mrlCode").val();
        var lotCode = $("#lotCode").val();
        var qty = $("#qty").val();
        var boxnum = $("#boxnum").val();

        //发货单
        if (fhd == "" || fhd == null || fhd == undefined) {
            return;
        }

        //托码
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            return;
        }

        //条码
        if (tm == "" || tm == null || tm == undefined) {
            return;
        }
        //物料号非空校验
        if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
            return;
        }
        //批次号非空校验
        if (lotCode == "" || lotCode == null || lotCode == undefined) {
            return;
        }

        //数量非空校验
        if (qty == "" || qty == null || qty == undefined || parseFloat(qty) <= 0) {
            return;
        }

        //箱号非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            return;
        }

        save();
    }


    //扫描条码
    function toScanner(flag) {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            var sm = ret;
            //托码
            if (flag == 1) {
                var text = document.getElementById("trayyinfoid");
                text.value = sm;

                //条码
            } else if(flag == 2){

                //条码解析
                tmSplit(sm);
            }else if(flag == 3){

              var text = document.getElementById("fhd");
              text.value = sm;
            }
        });
    }

    //条码监听回车事件
    $("#tm").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            //条码解析
            tmSplit(tm);
        }
    });

    //发货单监听回车事件
    $("#fhd").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            $("#fhd").val(tm);
        }
    });

    //托码监听回车事件
    $("#trayyinfoid").on('keypress', function(e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            $("#trayyinfoid").val(tm);
        }
    });

    //扫描之后条码解析
    function tmSplit(sm) {
        $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        $("#supplierCode").val(arr[0]);
        $("#mrlCode").val(arr[1]);
        $("#qty").val(arr[2]);
        $("#lotCode").val(arr[3]);
        $("#boxnum").val(arr[9]);
        //箱号选中
        $("#tm").select();

        //自动保存
        saveAuto();
    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }

                    //自动保存
                    saveAuto();
                } else if (myobj.code == '1') {

                    $("#tm").val("");
                    $("#tm").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
